package com.voice.call.utils;

import java.io.BufferedReader;  
import java.io.DataOutputStream;  
import java.io.IOException;  
import java.io.InputStream;  
import java.io.InputStreamReader;  
import java.io.OutputStreamWriter;  
import java.net.HttpURLConnection;  
import java.net.URI;  
import java.net.URISyntaxException;  
import java.net.URL;  
import java.net.URLConnection; 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HttpsURLConnection; 

public class HttpsTLS2Util {
	
	
	private static HttpURLConnection createConnection(URI uri) throws IOException {  
        URL url = uri.toURL();  
        URLConnection connection = url.openConnection();  
        HttpsURLConnection httpsURLConnection = (HttpsURLConnection) connection;  
        httpsURLConnection.setSSLSocketFactory(new TLSSocketConnectionFactory());  
        return httpsURLConnection;  
    }
	
	private static String getResponse(HttpURLConnection Conn) throws IOException {  
		  
		  
        InputStream is;  
        if (Conn.getResponseCode() >= 400) {  
            is = Conn.getErrorStream();  
        } else {  
            is = Conn.getInputStream();  
        }  
  
  
        String response = "";  
        byte buff[] = new byte[512];  
        int b = 0;  
        while ((b = is.read(buff, 0, buff.length)) != -1) {  
            response += new String(buff, 0, b);  
  
        }  
        is.close();  
        return response;  
    }  
	
	public static String doPostTSL2(String url,Map<String, Object> params){
		
		  String jsonStr="";  
		 HttpURLConnection connection;  
	        try {  
	            connection = createConnection(new URI(url));  
	            connection.setDoOutput(true);  
	            connection.setDoInput(true);  
	            connection.setRequestMethod("POST");  
	            connection.setUseCaches(false);  
	            connection.setInstanceFollowRedirects(true);  
	            connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
	  
	            connection.connect();  
	            
	            String bodyData = getParamsValues(params);
	            //POST请求  
	            OutputStreamWriter os = null;  
	          
	            os = new OutputStreamWriter(connection.getOutputStream());  
	            os.write(bodyData);  
	            os.flush();  
	            jsonStr=getResponse(connection);  
	            System.out.println(jsonStr);  
	  
	   
	             if (connection != null) {  
	                    connection.disconnect();  
	                }  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch (URISyntaxException e) {  
	            e.printStackTrace();  
	        }  
	        return jsonStr;
	}
	
	public static String doGetTSL2(String url,Map<String, Object> params){
		
		  String jsonStr="";  
		 HttpURLConnection connection;  
	        try { 
	        	
	        	String urlStr = url;
	        	if (params!=null && !params.isEmpty()) {
	        		urlStr += "?"+getParamsValues(params);
				}
	        	
	            connection = createConnection(new URI(urlStr));  
	            connection.setDoOutput(true);  
	            connection.setDoInput(true);  
	            connection.setRequestMethod("GET");  
	            connection.setUseCaches(false);  
	            connection.setInstanceFollowRedirects(true);  
	           // connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");  
	  
	            connection.connect();
	            
	            int code=connection.getResponseCode();
	            if (code == 200) {
	            	 jsonStr=getResponse(connection);  
	 	            //System.out.println(jsonStr);  
	 	            
	 	            
				}else {
					//System.out.println("请求失败:"+code);  
				}
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } catch (URISyntaxException e) {  
	            e.printStackTrace();  
	        }  
	        return jsonStr;
	}
	
	private static String getParamsValues(Map<String, Object> params) {
		
		List<String> list = new ArrayList<String>();
		if (params!=null && !params.isEmpty()) {
			
			for (Map.Entry<String, Object> entry : params.entrySet()) { 
				list.add(String.format("%s=%s", entry.getKey(),entry.getValue()));
			}
		}
		return org.apache.commons.lang.StringUtils.join(list.toArray(),"&");
	}

}
